/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.bean.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.genia.toolbox.basics.bean.Delimiter;
import com.genia.toolbox.basics.exception.technical.TechnicalIOException;
import com.genia.toolbox.basics.manager.CollectionManager;
import com.genia.toolbox.persistence.hibernate.bean.Queryable;
import com.genia.toolbox.persistence.hibernate.criteria.HibernateCriteria;

/**
 * Represents a generic Queryable that has sub-Queryable. It uses a
 * <code>Delimiter</code> to recursively generate the query and pass to its
 * sub-Queryable the parameters to fill.
 * 
 * @param <TYPE>
 *          the base type of its sub-Queryable
 */
public class DefaultQueryable<TYPE extends Queryable>
    implements Queryable
{

  /**
   * the <code>Delimiter</code> used to assemble the sub queries.
   */
  protected final transient Delimiter delimiter;

  /**
   * the sub-Queryables.
   */
  protected final transient List<TYPE> subQueryables;



  /**
   * Constructor.
   * 
   * @param delimiter
   *          the <code>Delimiter</code> used to assemble the sub queries
   * @param subQueryables
   *          the sub-Queryables
   */
  protected DefaultQueryable(final Delimiter delimiter, final TYPE... subQueryables)
  {
    this.delimiter = delimiter;
    this.subQueryables = new ArrayList<TYPE>(Arrays.asList(subQueryables));
  }



  /**
   * append to stringBuilder the part of the query represented by this object.
   * 
   * @param canHaveParameter
   *          can this part of the query use named parameters?
   * @param collectionManager
   *          The <code>CollectionManager</code> that allow us to perform
   *          joins
   * @param criteria
   *          the <code>HibernateCriteria</code> the represents the all query
   * @param stringBuilder
   *          the <code>StringBuilder</code> to append to.
   * @param queryableDisplayObject
   *          the <code>DisplayObject</code> that allows to convert this
   *          <code>Queryable</code> to a <code>String</code>
   */
  public void appendQuery(final boolean canHaveParameter, final CollectionManager collectionManager, final HibernateCriteria<?> criteria, final StringBuilder stringBuilder,
      final QueryableDisplayObject queryableDisplayObject)
  {
    try {
      collectionManager.join(stringBuilder, subQueryables, delimiter, queryableDisplayObject);
    }
    catch (final TechnicalIOException e) {
      assert false : "Can't happen";
    }
  }



  /**
   * Fill the parameters <code>Map</code> with the constants elements of this
   * query.
   * 
   * @param parameters
   *          the <code>Map</code> to be filled
   */
  public void fillParameters(final Map<String, Object> parameters)
  {
    for (final Queryable subQueryable : subQueryables) {
      subQueryable.fillParameters(parameters);
    }
  }



  /**
   * getter for the delimiter property.
   * 
   * @return the delimiter
   */
  public Delimiter getDelimiter()
  {
    return delimiter;
  }



  /**
   * getter for the subQueryables property.
   * 
   * @return the subQueryables
   */
  public List<TYPE> getSubQueryables()
  {
    return subQueryables;
  }

}
